﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LevenBlog.Core.Data;
using LevenBlog.Core.Entities;

namespace LevenBlog.Data.SqlServer
{
    public sealed class SqlUserInfo : IUserInfo
    {
        public void Insert(IDbProvider provider, lb_userinfo info)
        {
            IDataParameter[] paras = null;
            string sql = SqlHelper.Instance.CreateInsertSql(info, out paras);
            SqlHelper.Instance.ExecuteNonQuery(provider, sql, paras);
        }

        public void Update(IDbProvider provider, lb_userinfo info)
        {
            IDataParameter[] paras = null;
            string sql = SqlHelper.Instance.CreateUpdateSql(info, out paras);
            SqlHelper.Instance.ExecuteScalar(provider, sql, paras);
        }

        public void Delete(IDbProvider provider, int id)
        {
            string sql = string.Format("delete form [lb_userinfo] where [id]={0}", id);
            SqlHelper.Instance.ExecuteNonQuery(provider, sql);
        }

        public void DeleteFromUserID(IDbProvider provider, int userid)
        {
            string sql = string.Format("delete from [lb_userinfo] where [userid]={0}", userid);
            SqlHelper.Instance.ExecuteNonQuery(provider, sql);
        }

        public lb_userinfo Get(IDbProvider provider, int id)
        {
            string sql = string.Format("select * from [lb_userinfo] where [id]={0}", id);
            using (IDataReader dr = SqlHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqlHelper.Instance.ConvertToEntity<lb_userinfo>(dr);
            }
        }

        public lb_userinfo GetFromUserID(IDbProvider provider, int userid)
        {
            string sql = string.Format("select * from [lb_userinfo] where [userid]={0}", userid);
            using (IDataReader dr = SqlHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqlHelper.Instance.ConvertToEntity<lb_userinfo>(dr);
            }
        }
    }
}
